package com.myCompany.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.myCompany.edu.realtime.app.BaseAppV1;
import com.myCompany.edu.realtime.bean.UserUserRegisterBean;
import com.myCompany.edu.realtime.common.Constant;
import com.myCompany.edu.realtime.util.AtguiguUtil;
import com.myCompany.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @ClassName：Dws05_DwsUserUserRegister
 * @Author: xxx
 * @Date: 2022/10/21 18:41
 * @Description:
 */
public class Dws05_DwsUserUserRegister extends BaseAppV1 {

    public static void main(String[] args) {
        new Dws05_DwsUserUserRegister().init(
                30051,
                2,
                "Dws05_DwsUserUserRegister",
                Constant.TOPIC_DWD_USER_REGISTER
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream
                .map(json -> {
                    long ts = AtguiguUtil.dateTimeToTs(JSON.parseObject(json).getString("create_time"));
                    return new UserUserRegisterBean("", "",
                            1L,
                            ts
                    );
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<UserUserRegisterBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((bean, ts) -> bean.getTs())
                )
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<UserUserRegisterBean>() {
                            @Override
                            public UserUserRegisterBean reduce(UserUserRegisterBean value1, UserUserRegisterBean value2) throws Exception {
                                value1.setRegisterCt(value1.getRegisterCt() + value2.getRegisterCt());
                                return value1;
                            }
                        },
                        new ProcessAllWindowFunction<UserUserRegisterBean, UserUserRegisterBean, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<UserUserRegisterBean, UserUserRegisterBean, TimeWindow>.Context ctx, Iterable<UserUserRegisterBean> elements, Collector<UserUserRegisterBean> out) throws Exception {
                                UserUserRegisterBean bean = elements.iterator().next();

                                bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));

                                bean.setTs(System.currentTimeMillis());

                                out.collect(bean);
                            }
                        })
                .addSink(FlinkSinkUtil.getClickHouseSink("dws_user_user_register_window", UserUserRegisterBean.class));
    }
}
